import React, { useState } from 'react';
import { Modal, Form, Input, Button, message } from 'antd';
import { resetPassword } from '@/utils/api/student/student';

interface ResetPasswordModalProps {
    visible: boolean;
    onCancel: () => void;
    onSuccess: () => void;
    studentId: number;
    studentName: string;
}

const ResetPasswordModal: React.FC<ResetPasswordModalProps> = ({
                                                                   visible,
                                                                   onCancel,
                                                                   onSuccess,
                                                                   studentId,
                                                                   studentName
                                                               }) => {
    const [form] = Form.useForm();
    const [loading, setLoading] = useState(false);

    const handleSubmit = async () => {
        try {
            const values = await form.validateFields();
            setLoading(true);

            const response = await resetPassword(studentId, values.password);
            if (response.code === 200) {
                message.success('密码重置成功');
                form.resetFields();
                onSuccess();
                onCancel();
            }
        } catch (error) {
            console.error('密码重置失败:', error);
        } finally {
            setLoading(false);
        }
    };

    return (
        <Modal
            title={`重置密码 - ${studentName}`}
            open={visible}
            onCancel={() => {
                onCancel();
                form.resetFields();
            }}
            footer={[
                <Button key="cancel" onClick={() => {
                    onCancel();
                    form.resetFields();
                }}>
                    取消
                </Button>,
                <Button
                    key="submit"
                    type="primary"
                    loading={loading}
                    onClick={handleSubmit}
                >
                    确定
                </Button>,
            ]}
            width={400}
        >
            <Form
                form={form}
                layout="vertical"
            >
                <Form.Item
                    name="password"
                    label="新密码"
                    rules={[
                        { required: true, message: '请输入新密码' },
                        { min: 6, message: '密码长度不能少于6位' }
                    ]}
                >
                    <Input.Password placeholder="请输入新密码" />
                </Form.Item>

                <Form.Item
                    name="confirmPassword"
                    label="确认密码"
                    dependencies={['password']}
                    rules={[
                        { required: true, message: '请确认密码' },
                        ({ getFieldValue }) => ({
                            validator(_, value) {
                                if (!value || getFieldValue('password') === value) {
                                    return Promise.resolve();
                                }
                                return Promise.reject(new Error('两次输入的密码不一致'));
                            },
                        }),
                    ]}
                >
                    <Input.Password placeholder="请再次输入密码" />
                </Form.Item>
            </Form>
        </Modal>
    );
};

export default ResetPasswordModal;